package ig.metrica.ejb.html;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.ejb.*;

/**
 * This is the bean class for the ControlGenerarHTMLBean enterprise bean.
 * Created 5/03/2006 03:54:25 PM
 * @author jsanchez
 */
public class ControlGenerarHTMLBean{
	static final long serialVersionUID = 01;
	private SessionContext context=null;
        private boolean inicio=true;
        private StringBuffer _HTML_=new StringBuffer();
        private StringBuffer html=null;
        private String pathMetricaWar="";
        boolean ok=false;
        private Integer secuencia=0;
        private String directorio=null;
    public String getDirectorio() {
            return directorio+"_"+secuencia;
    }

    private void loadPathMetrica(){
        File file = new File("");
        file = new File("../server/default/tmp/deploy");
        String[] lista=file.list();
        for (int i = 0; i < lista.length; i++) {
            if(lista[i].endsWith("ig-metrica-web-exp.war") || lista[i].endsWith("ig-metrica-web.war")){
             pathMetricaWar=lista[i];
             break;
            }
        }
        ok=true;
    }

    private synchronized void guardar(String path){
        _HTML_.append("<html>\n");
        _HTML_.append("<head>\n");
        _HTML_.append("<title>OutPut_Server</title>\n");
        _HTML_.append("</head>\n");
        _HTML_.append("<body>\n");
        BufferedWriter br =null;
        FileOutputStream fileOutRunBat=null;
        try {
                fileOutRunBat = new FileOutputStream(path);
                br = new BufferedWriter(new OutputStreamWriter(fileOutRunBat));
            _HTML_.append(html+"\n");
            _HTML_.append("</body>\n");
            _HTML_.append("</html>\n");
            br.write(_HTML_.toString());
            br.flush();
            br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
    public void crearDirectorio(String directorio,boolean crear){
            if(!ok)loadPathMetrica();
            if(inicio){
                inicio=false;
                File file = new File("../server/default/tmp/deploy/"+pathMetricaWar+"/"+directorio+"_"+secuencia);
                while(file.exists()){
                    secuencia++;
                    file = new File("../server/default/tmp/deploy/"+pathMetricaWar+"/"+directorio+"_"+secuencia);
                }
                if(crear)
                    file.mkdir();
            }
            this.directorio=directorio;
    }
    public void generarHTML(String directorio, String nombre,StringBuffer html) {
            this.html=html;
             File file = new File("../server/default/tmp/deploy/"+pathMetricaWar+"/"+directorio+"_"+secuencia+"/"+nombre+".html");
             String path=file.getAbsolutePath();
             _HTML_= new StringBuffer("");
             guardar(path);
    }
}
